<template>
  <div class="add_container" style="height: 100px;">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card >
      <!-- 提示区域 -->
      <el-alert title="添加商品信息" type="info" center show-icon :closable="false"></el-alert>
      <!-- 步骤条区域 -->
      <el-steps :space="200" :active="activeIndex-0" finish-status="success" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- tab 栏区域 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px" label-position="top">
        <el-tabs v-model="activeIndex" tab-position="left">
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称" prop="goodName">
              <el-input v-model='addForm.goodName'></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="price">
              <el-input v-model='addForm.price' type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="stock">
              <el-input v-model='addForm.stock' type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" >
              <!-- 级联选择器 -->
                <el-select v-model="addForm.catId" value-key="catId" @change="changeCheckWay" placeholder="请选择">
                  <el-option
                    v-for="item in cateList"
                    :key="item.catId"
                    :label="item.catName"
                    :value="item.catId">
                  </el-option>
                </el-select>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <!-- 富文本编辑器组件 -->
            <quill-editor v-model="addForm.content"></quill-editor>
            <!-- 添加商品的按钮 -->
            <el-button class="btnAdd" type="primary" @click="add">添加商品</el-button>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { messageTip } from '@/api/messageTipAPI.js'
import _ from 'lodash'
export default {
  name: 'Add',
  data () {
    return {
      // 进度条和左侧tab栏的激活项
      activeIndex: '0',
      // 添加商品的表单数据对象（将来调用 api 添加商品是用来当做参数传给后台）
      addForm: {
        goodName: '',
        price: 0,
        stock: 1,
        catId: '', // 商品所属的分类数组
        content: '' // 商品详情描述,
      },
      addFormRules: {
        goodsName: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        price: [{ required: true, message: '请输入商品价格', trigger: 'blur' }],
        stock: [{ required: true, message: '请输入商品数量', trigger: 'blur' }],
        catId: [{ required: true, message: '请选择商品分类', trigger: 'blur' }]
      },
      // 商品分类数据列表
      cateList: [],
      cateProps: {
        expandTrigger: 'hover',
        label: 'catName',
        value: 'catId'
      }
    }
  },
  methods: {
    // 获取所有商品分类数据
    changeCheckWay(value){
      this.addForm.catId = value
    },
    async getCateList () {
      const { data: res } = await this.$http.get('/categories')
      if (res.meta.status !== 200) return messageTip('error', '获取商品分类数据失败！')
      // 请求成功的操作
      this.cateList = res.data
      console.log(this.cateList)
    },
    // 添加商品
    add () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return messageTip('error', '请填写必须的表单项！')
        /** 预校验成功的操作，发起添加的请求 */
        const form = _.cloneDeep(this.addForm)
        console.log(form)
        const { data: res } = await this.$http.post('/good/addgood', form)
        if (res.meta.status !== 200) return messageTip('error', '添加商品失败！')
        // 请求成功的操作
        messageTip('success', '商品添加成功！')
        this.$router.push('/goods')
      })
    }
  },
  created () {
    this.getCateList()
  }
}
</script>

<style lang='less' scoped>
.el-checkbox{
  margin: 0 10px 0 0 !important;
}
.previewImg{
  width: 100%;
}
.btnAdd{
  margin-top: 15px;
}
</style>
